//
//  BaseTabBarViewController.swift
//  weibo
//
//  Created by 叶子韬韬 on 2017/4/10.
//  Copyright © 2017年 叶子韬韬. All rights reserved.
//

import UIKit

class BaseTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        

       // MARK:- 原始的创建底部tabbar
//        setBaseBar()
        // MARK:- #注释> 利用控制器字符串创建底部tabbar
        setBaesBarWithString()
        
    }

    func setBaseBar()  {
        
        addChildVC(nameVC: HomeViewController(), titleVC: "首页", imageVC: "tabbar_home", selectImageVC: "tabbar_home_highlighted")
        addChildVC(nameVC: DiscoverViewController(), titleVC: "首页", imageVC: "tabbar_discover", selectImageVC: "tabbar_discover_highlighted")
        addChildVC(nameVC: MessageViewController(), titleVC: "首页", imageVC: "tabbar_message_center", selectImageVC: "tabbar_message_center_highlighted")
        addChildVC(nameVC: ProfileViewController(), titleVC: "首页", imageVC: "tabbar_profile", selectImageVC: "tabbar_profile_highlighted")
    }

    private func addChildVC(nameVC:UITableViewController,titleVC:NSString,imageVC:NSString,selectImageVC:NSString)  {
        
        nameVC.title = titleVC as String
        nameVC.tabBarItem.image = UIImage(named: imageVC as String)
        nameVC.tabBarItem.selectedImage = UIImage(named: selectImageVC as String)
        
        let childNav = UINavigationController(rootViewController: nameVC)
        
        addChildViewController(childNav)

        
    }

    func setBaesBarWithString(){
        
        //获取本地json数据
        guard let jsonPath = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil) else{
            print("没有获取到文件路径")
            return
        }
        
        // 读取文件数据
        guard let jsondata = NSData(contentsOfFile: jsonPath) else{
            print("加载二进制失败")
            return
        }
        
        /**
         swift 三种处理异常
          方式-  do-catch try
          方式二  try？系统帮我处理异常，如果该方法出现了异常  返回nil
          方式三  try！ 直接告诉系统 该方法没有异常  如果出现异常 程序就奔溃了
        */
        
        //将数据转成
        guard let anyObject = try? JSONSerialization.jsonObject(with: jsondata as Data, options: .mutableContainers) else{
            
            return
        }
        guard let dictArray = anyObject as?[[String:AnyObject]] else {
            
            return
        }
        print(dictArray)
        
        print("--------------------")
        //遍历数组
        for dict in dictArray {
//            print(dict)
            
            guard let nameVC = dict["vcName"] as?String else {
                continue
            }
            guard let titleVC = dict["title"] as?String else {
                continue
            }
            guard let imageVC = dict["imageName"] as?String else {
                continue
            }
            
            addChildVCWithString(nameVC: nameVC, titleVC: titleVC, imageVC: imageVC, selectImageVC: imageVC+"_highlighted")
            
        }
        
        
        
        
//        addChildVCWithString(nameVC: "HomeViewController", titleVC: "首页", imageVC: "tabbar_home", selectImageVC: "tabbar_home_highlighted")
//        addChildVCWithString(nameVC: "DiscoverViewController", titleVC: "首页", imageVC: "tabbar_discover", selectImageVC: "tabbar_discover_highlighted")
//        addChildVCWithString(nameVC: "MessageViewController", titleVC: "首页", imageVC: "tabbar_message_center", selectImageVC: "tabbar_message_center_highlighted")
//        addChildVCWithString(nameVC: "PrifileViewController", titleVC: "首页", imageVC: "tabbar_profile", selectImageVC: "tabbar_profile_highlighted")

        
    }
    
    private func addChildVCWithString(nameVC:String,titleVC:String,imageVC:String,selectImageVC:String)  {
        
        print("\(nameVC)")
        
        //1 获取命名空间
        guard let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as?String else{
            
            print("没有获取到命名空间")
            
            return
            
        }
        //2 根据对应的字符串创建class
        guard let ChildClass = NSClassFromString(nameSpace+"."+nameVC) else {
            print("创建class失败")
            return
        }
        
        guard let ChildType = ChildClass as? UIViewController.Type else{
            
            print("转换控制器失败")
            
            return
        }
        
        //3 创建对应的控制器对象
        let ChildVC = ChildType.init()
        
        //4 创建对应的tabbar
        ChildVC.title = titleVC
        ChildVC.tabBarItem.image = UIImage(named: imageVC )
        ChildVC.tabBarItem.selectedImage = UIImage(named: selectImageVC)
        
        let childNav = UINavigationController(rootViewController: ChildVC)
        
        addChildViewController(childNav)
        
        
    }

    
    
    
}
